﻿
#region Usings

using FasterWPF;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.Common;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using WPFContrib;
using FasterWPF.Selectors;
#endregion

namespace WPFContribPresenters
{
    public class VoltagePointCollection : RingArray<VoltagePoint>
    {
        private const int TOTAL_POINTS = 300;

        public VoltagePointCollection()
            : base(TOTAL_POINTS) 
        {
        }
    }

    public class VoltagePoint
    {
        public DateTime Date { get; set; }

        public double Voltage { get; set; }

        public VoltagePoint(double voltage, DateTime date)
        {
            this.Date = date;
            this.Voltage = voltage;
        }
    }

    public partial class ChartUserControl : UserControl, IUserControlGuid
    {
        private string _userCtlGuid = null;

        public string UserControlGuid
        {
            get
            {
                return _userCtlGuid;
            }

            set
            {
                _userCtlGuid = value;
            }
        }

        public ChartUserControl()
        {
            //InitializeComponent(); XAML removed. Initialize via code-behind.
            
            this.Initialize(ContainerType.VerticalPanel);

            ActionAggregator.CreateActionStore<ChartUserControl>();

            // this.Width = 850D;
            // this.Height = 750D;

            //this.BeginSettings<UserControl>()
            //    .SetSelectorClass<UserControl>(0, 0, "tabLabel")
            //    .SetItemBorderColorAndThickness<UserControl>(Brushes.Gray, new Thickness(0)).EndSettings();
        }

        public void BuildAndStoreRemoveAction(ChartUserControl usc, UserControlRegion regionOne)
        {
            //Store Action in Global Store on Application object
            ActionAggregator.Put<ChartUserControl>("RegionOneRemove", () =>
                {
                    MessageBox.Show("I was just clicked: Remove me!");

                    usc.DisposeEventsOnAllParents();

                    //Remove UserControl from UserControlRegion
                    regionOne.RemoveAll<Border>();
                }
            );
        }

        public void BuildAndStoreAddAction(ChartUserControl usc, UserControlRegion regionOne)
        {
              ActionAggregator.Put<ChartUserControl>("RegionOneAdd", () =>
              {
                  MessageBox.Show("I was just clicked: Add me!");

                  if (regionOne.GetCompositeLookupDictionary<Border>() != null && regionOne.GetCompositeLookupDictionary<Border>().Count > 0)
                  {
                       usc.DisposeEventsOnAllParents();

                       //If it hasn't already been removed, remove UserControl from UserControlRegion
                       regionOne.RemoveAll<Border>();
                   }
                                     
                   ChartUserControl uscNext = new ChartUserControl();
                   uscNext.UserControlGuid = _userCtlGuid;
                   regionOne.AddUserControl(uscNext);

                   uscNext.BuildAndStoreRemoveAction(uscNext, regionOne);
                   uscNext.BuildAndStoreAddAction(uscNext, regionOne);

                   uscNext.LoadUserControl();
              });
        }

        public void LoadUserControl()
        {
            //Canvas userControlCanvas = new Canvas();
            //userControlCanvas.Initialize(ContainerType.ContentControlPanel);

            this.BeginSettings<UserControl>().SetItemBorderColorAndThickness<UserControl>(Brushes.Gray, new Thickness(3)).EndSettings();

            ChartPlotter crp = new ChartPlotter();
            IPlotterElement horizAxis = new Microsoft.Research.DynamicDataDisplay.HorizontalAxisTitle() { Background = Brushes.AliceBlue, BorderBrush = Brushes.DarkBlue };
            crp.Children.Add(horizAxis);
            VoltagePointCollection vpc = new VoltagePointCollection();
            vpc.Add(new VoltagePoint(1.0D, DateTime.Now.AddDays(1)));
            vpc.Add(new VoltagePoint(2.0D, DateTime.Now.AddDays(2)));
            vpc.Add(new VoltagePoint(7.0D, DateTime.Now.AddDays(3)));
            vpc.Add(new VoltagePoint(10.0D, DateTime.Now.AddDays(4)));

            var ds = new EnumerableDataSource<VoltagePoint>(vpc);
            ds.SetXMapping(x => (double)(x.Date.Day));
            ds.SetYMapping(y => y.Voltage);
            crp.AddLineGraph(ds, Colors.Green, 2, "Volts"); // to use this method you need to add manually "using Microsoft.Research.DynamicDataDisplay;"

            this.BeginComposite<UserControl>(_userCtlGuid)
                .AddLabel<UserControl>(0, 0, "I am a User Control", Brushes.LightGreen)
                .AddFlatButton<UserControl>(0, 1, double.NaN, double.NaN,
                "Remove this UserControl!", Brushes.Gray, new Thickness(2), Brushes.Silver, new CornerRadius(0), new Thickness(2), new Thickness(3), Brushes.LightSlateGray, Brushes.White,
                (o, ev) =>
                {
                    ActionAggregator.Fire<ChartUserControl>("RegionOneRemove");
                })
                  .AddExisting(0, 2, crp)
                  .EndComposite<UserControl, UserControlArgs>(null);     
        }

    }//end of class
}
